//
//  GHWeiboEngine.h
//  notebook
//
//  Created by 诗彬 刘 on 12-5-9.
//  Copyright (c) 2012年 ifuninfo. All rights reserved.
//

#import <Foundation/Foundation.h>

#define kHttpMethodGet         @"GET"
#define kHttpMethodPOST        @"POST"

#define     WBRequestInvalidMethod          
#define     WBRequestPublicTimeline         @"statuses/public_timeline.json"
#define     WBRequestHomeTimeline           @"statuses/home_timeline.json"
#define     WBRequestFriendsTimeline        @"statuses/friends_timeline.json"
//#define     WBRequestFriendsTimeLine_ids   
//#define     WBRequestUserTimeline 
//#define     WBRequestUserTimeline_ids
//#define     WBRequestRepostTimeline
//#define     WBRequestRepostTimeline_ids
//#define     WBRequestRepostByMe
//#define     WBRequestMentions
//#define     WBRequestMentions_ids
//#define     WBRequestBilateralTimeline
//#define     WBRequestShow
//#define     WBRequestQueryMid
//#define     WBRequestQueryId
//#define     WBRequestHotRepostDaily
//#define     WBRequestHotRepostWeekly
//#define     WBRequestHotCommentsDaily
//#define     WBRequestHotCommentsWeekly
//#define     WBRequestCount
//    
//#define     WBRequestRepost
//#define     WBRequestDestroy
//#define     WBRequestUpdate
//#define     WBRequestUpload
//#define     WBRequestUploadUrlText
//#define     WBRequestEmotions

@interface NSString(WeiboUlti)
+ (NSString *)WBParamstringWithDictionary:(NSDictionary *)dic;
+ (NSString *)WBRequestURLWithBaseURL:(NSString *)baseURL params:(NSDictionary *)params httpMethod:(NSString *)httpMethod;
- (NSString *)WBUrlEncodeString;
@end

typedef enum{
	kWBErrorCodeInterface	= 100,
	kWBErrorCodeSDK         = 101,
}WBErrorCode;

typedef enum{
	kWBSDKErrorCodeParseError       = 200,
	kWBSDKErrorCodeRequestError     = 201,
	kWBSDKErrorCodeAccessError      = 202,
	kWBSDKErrorCodeAuthorizeError	= 203,
    kWBSDKErrorCodeRequestCanceled  = 204,
}WBSDKErrorCode;

typedef enum{
    kWBRequestPostDataTypeNone = 0,
	kWBRequestPostDataTypeNormal,			// for normal data post, such as "user=name&password=psd"
	kWBRequestPostDataTypeMultipart,        // for uploading images and files.
}WBRequestPostDataType;

//WeiboRequest
@class GHWeiboRequest;
@class GHWeiboEngine;

typedef void (^GHWeiboBasicBlock)(void);
typedef void (^GHWeiboBlock)(GHWeiboEngine *weiboEngine);

@protocol GHWeiboRequestDelegate <NSObject>
@optional
- (void)WeiboRequest:(GHWeiboRequest *)request didReceiveResponse:(NSURLResponse *)response;
- (void)WeiboRequest:(GHWeiboRequest *)request didReceiveRawData:(NSData *)data;
- (void)WeiboRequest:(GHWeiboRequest *)request didFailWithError:(NSError *)error;
- (void)WeiboRequest:(GHWeiboRequest *)request didFinishLoadingWithResult:(id)result;
@end

extern NSString *const kWBRequestResultData;
extern NSString *const kWBRequestResultJsonData;
extern NSString *const kWBRequestResultError;
extern NSString *const kWBRequestResultStatus;

@interface GHWeiboRequest:NSOperation{
    WBRequestPostDataType postDataType;
    NSString *url;
    NSDictionary *params;
    NSString *httpMethod;
    NSDictionary *httpHeaderFields;
    NSString *identifier;
    
    NSDictionary *requestResult;
    
    BOOL     needAuthorize;
}
@property(nonatomic,readonly)NSString *url;
@property(nonatomic,readonly)NSDictionary *params;
@property(nonatomic,readonly)NSString *identifier;
@property(nonatomic,readonly)NSDictionary *requestResult;
@property(nonatomic,assign)GHWeiboEngine *weiboEngine;
@property(nonatomic,retain)id<GHWeiboRequestDelegate> delegate;
+ (GHWeiboRequest *)requestWithURL:(NSString *)url 
                        httpMethod:(NSString *)httpMethod 
                            params:(NSDictionary *)params
                      postDataType:(WBRequestPostDataType)postDataType
                  httpHeaderFields:(NSDictionary *)httpHeaderFields
                        identifier:(NSString *)identifier
                          delegate:(id<GHWeiboRequestDelegate>)delegate;

+ (GHWeiboRequest *)authorizedRequestWithURL:(NSString *)url
                                  httpMethod:(NSString *)httpMethod 
                                      params:(NSDictionary *)params
                                postDataType:(WBRequestPostDataType)postDataType
                            httpHeaderFields:(NSDictionary *)httpHeaderFields
                                  identifier:(NSString *)identifier
                                    delegate:(id<GHWeiboRequestDelegate>)delegate;

+ (GHWeiboRequest *)apiRequestWithMethod:(NSString *)mothod
                              httpMethod:(NSString *)httpMethod 
                                  params:(NSDictionary *)params
                            postDataType:(WBRequestPostDataType)postDataType
                        httpHeaderFields:(NSDictionary *)httpHeaderFields
                              identifier:(NSString *)identifier
                                delegate:(id<GHWeiboRequestDelegate>)delegate
                           needAuthorize:(BOOL)need;

+ (GHWeiboRequest *)uploadApiRequestWithMethod:(NSString *)method
                                    httpMethod:(NSString *)httpMethod
                                        params:(NSDictionary *)params
                                  postDataType:(WBRequestPostDataType)postDataType
                              httpHeaderFields:(NSDictionary *)httpHeaderFields
                                    identifier:(NSString *)identifier
                                      delegate:(id<GHWeiboRequestDelegate>)delegate
                                 needAuthorize:(BOOL)need;

- (id)initWithURL:(NSString *)url 
       httpMethod:(NSString *)httpMethod 
           params:(NSDictionary *)params
     postDataType:(WBRequestPostDataType)postDataType
 httpHeaderFields:(NSDictionary *)httpHeaderFields
       identifier:(NSString *)identifier
         delegate:(id<GHWeiboRequestDelegate>)delegate
    needAuthorize:(BOOL)need;
@end

//WeiboEngine
@protocol GHWeiboEngineDelegate <NSObject>
@optional

- (void)engineAlreadyLoggedIn:(GHWeiboEngine *)engine;

- (void)engineDidLogIn:(GHWeiboEngine *)engine;

- (void)engine:(GHWeiboEngine *)engine didFailToLogInWithError:(NSError *)error;

- (void)engineDidLogOut:(GHWeiboEngine *)engine;

- (void)engineNotAuthorized:(GHWeiboEngine *)engine;

- (void)engineAuthorizeExpired:(GHWeiboEngine *)engine;
@end

extern NSString *const kGHWeiboEngineLoginedNotification;

#define  WB_ENGINE [GHWeiboEngine sharedGHWeiboEngine]

@interface GHWeiboEngine : NSObject{
    NSString         *appKey;
    NSString         *appSecret;
    
    NSString         *userID;
    NSString         *accessToken;
    NSTimeInterval   expireTime;
    
    NSString         *redirectURI;
    
    BOOL             logging;
    
    NSOperationQueue *requests;
}

@property (nonatomic, readonly) NSString *appKey;
@property (nonatomic, readonly) NSString *appSecret;
@property (nonatomic, readonly) NSString *userID;
@property (nonatomic, readonly) NSString *accessToken;
@property (nonatomic, readonly) NSTimeInterval expireTime;
@property (nonatomic, retain) NSString *redirectURI;
@property (nonatomic, assign,getter = isUserExclusive) BOOL userExclusive;
@property (nonatomic, readonly) BOOL isLogined;
@property (nonatomic, readonly) NSOperationQueue *requests;
@property (nonatomic, assign) id<GHWeiboEngineDelegate> delegate;

+ (GHWeiboEngine *)sharedGHWeiboEngine;

//- (id)initWithAppKey:(NSString *)theAppKey appSecret:(NSString *)theAppSecret;
- (void)requestAccessTokenWithUserID:(NSString *)userID_ password:(NSString *)password;
- (void)logIn;
- (void)logInUsingUserID:(NSString *)theUserID password:(NSString *)thePassword;
- (void)logOut;
- (BOOL)isAuthorizeExpired;

- (void)addRequest:(GHWeiboRequest *)request;
- (void)cancelAllRequest;
- (void)cancelRequestWithIdentifier:(NSString *)identifier;
- (NSArray *)requestsWithIdentifer:(NSString *)identifier;
@end
